package com.systop.cardecorate.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systop.cardecorate.admin.entity.CaptchaEntity;
import com.systop.cardecorate.admin.service.CaptchaService;
import com.systop.common.utils.DateUtils;
import com.systop.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jdk.nashorn.internal.runtime.Undefined;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jsj
 * @date 2022/5/5
 */
@RestController
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;

    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    @Operation(summary = "获取验证码")
    // @ApiOperation("获取验证码")
    public void captcha(HttpServletResponse response, String uuid)throws IOException {
        if(uuid != "undefined" && uuid != ""){
            response.setHeader("Cache-Control", "no-store, no-cache");
            response.setContentType("image/jpeg");

            //获取图片验证码
            BufferedImage image = captchaService.getCaptcha(uuid);

            //转换成流形式传输到前台
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
            IOUtils.closeQuietly(out);
        }else{

        }
    }

    //过期验证码删除
    @GetMapping("/overdueCaptcha")
    @Operation(summary = "校验验证码")
    public void overdueCaptcha(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        double overdueTime = System.currentTimeMillis()-60;

        //开始时间
        String startTime = "2022-01-01 00:00:00";
        String format = sdf.format(overdueTime);
        // System.out.println(overdueTime+","+format);

        captchaService.overdueCaptcha(startTime,format);
    }

}
